﻿#include "PreloopBlock.h"
#include "QBlock.h"
namespace CVV
{
namespace FlowChart
{

PreloopBlock::PreloopBlock(QString &name, QRect &position, double zoom, bool isExpand, QMargins margins)
{
    setZoom(zoom);
    setPostion(position);
    setMargin(margins);
    setName(name);
    m_isExpand = isExpand;
}

PreloopBlock::~PreloopBlock()
{

}

void PreloopBlock::paint(QPainter *canvas)
{
    double y=postion().y();
    double x=postion().x();
    double width=postion().width();
    double height=postion().height();
    double hcenter = x + width / 2;
    double a = static_cast<double>(m_aScale)* zoom();
    double b = static_cast<double>(m_bScale) * a;
    double lineScale=static_cast<double>(m_lineScale);
    double bottom = y + height;

    //    qDebug()<<"Pre->branchitems:"<<items.size();
    /* 有条件循环 */
    SetNewPen(2)
    canvas->drawLine(QLineF(hcenter, y-0.5, hcenter, y + lineScale * zoom()));
    drawBottomArrow(canvas, QPointF(hcenter, y + lineScale * zoom()),
                    QSize(static_cast<int>(6 * zoom()), static_cast<int>(12 * zoom())));

    QPointF par[3][4];
    for(int i = 0 ; i < 3; i++)
    {
        par[i][0] = QPointF(hcenter - b/2 + 3*i, y + m_lineScale * zoom() + a/2 + 3*i);
        par[i][1] = QPointF(hcenter + 3*i      , y + m_lineScale * zoom() + 3*i);
        par[i][2] = QPointF(hcenter + b/2 + 3*i, y + m_lineScale * zoom() + a/2 + 3*i);
        par[i][3] = QPointF(hcenter + 3*i      , y + m_lineScale * zoom() + a   + 3*i);
    }

    QColor bgColor = m_backgroundColor;
    if(!m_isExpand)
    {
        bgColor = m_collapseBgColor;
    }
    QBrush brush = canvas->brush();
    canvas->setBrush(bgColor);
    if(m_isExpand)
    {
        canvas->drawPolygon(par[0], 4);
    }
    else{
        for(int di = 2; di >= 0; di--)
        {
            canvas->drawPolygon(par[di], 4);
        }
    }
    canvas->setBrush(brush);

    UseOldPen
    QRectF rect(hcenter - b/2, y + lineScale * zoom(), b, a);
    canvas->drawText(rect, Qt::AlignCenter | Qt::TextWrapAnywhere, QString("%1").arg(name()));
    if(m_isExpand)
    {
        QBlock *left = getBranchItem().at(0);
        Q_ASSERT_X(left != nullptr, "QBlock::paint()" ,"item(0) == 0. i.e. body of PRE-loop is nul.");

        SetNewPen(2)

        canvas->drawLine(QLineF(hcenter,y + lineScale * zoom() + a, hcenter, left->m_dY));
        //        // Right line
        QPointF line[5];
        line[0] = QPointF(hcenter + b/2, y + lineScale * zoom() + a/2);
        line[1] = QPointF(x + width - 5*zoom(), y + lineScale * zoom() + a/2);
        line[2] = QPointF(x + width - 5*zoom(), bottom - 4 * zoom());
        line[3] = QPointF(hcenter, bottom - 4 * zoom());
        line[4] = QPointF(hcenter, bottom+0.5);
        canvas->drawPolyline(line, 5);

        // connect
        QPointF collector[5];
        collector[0] = QPointF(hcenter, left->m_dY + left->m_dHeight);
        collector[1] = QPointF(hcenter, bottom - 28*zoom());
        collector[2] = QPointF(x + 5*zoom(), bottom - 28*zoom());
        collector[3] = QPointF(x + 5*zoom(), y + 8*zoom());
        collector[4] = QPointF(hcenter, y + 8*zoom());
        canvas->drawPolyline(collector, 5);
        drawRightArrow(canvas, collector[4],QSize(static_cast<int>(12 * zoom()), static_cast<int>(6 * zoom())));

        UseOldPen

        canvas->setPen(Qt::white);
        canvas->drawText(QPointF(hcenter + 4*zoom(), y + 44 * zoom() + a), QObject::tr("Yes"));
        canvas->drawText(QPointF(hcenter + b/2 + 5*zoom(), y + 28 * zoom() + a/2), QObject::tr("No"));
    }
    else
    {
        UseOldPen
        canvas->setPen(Qt::white);
        canvas->drawText(QPointF(hcenter + b/3, y + 12 * zoom() + a/3), QObject::tr("Fold"));
    }
}

QString PreloopBlock::getBlockDom()
{
    QString xmlStr = QString("<algorithm><branch><pre cond=\"x &lt; n\" id=\"%1\" category=\"%2\" code=\"%3\" title=\"%4\" isPlugin=\"true\"><branch /></pre></branch></algorithm>");
    return  xmlStr;
}
}
}
